package com.lagou.homework.servlet;

import com.lagou.homework.bean.Student;
import com.lagou.homework.service.StudentManageService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "SearchServlet", urlPatterns = "/searchServlet")
public class SearchServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 0.设置编码方式
        request.setCharacterEncoding("utf-8");

        // 1.创建StudentManageService对象
        StudentManageService studentManageService = new StudentManageService();
        // 2.从request中获取要查询的学生的学号
        int code = Integer.parseInt(request.getParameter("search").trim());
        // 3.执行查询操作
        Student student = studentManageService.searchStudentService(code);
        // 4.创建一个Student数组，将结果放入数组中
        Student[] allStudents = {student};
        // 2.将获取到的数组传入session，实现浏览器跳转，跳转到主页面
        request.getSession().setAttribute("allStudents", allStudents);
        response.sendRedirect("main.jsp");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
